package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.xinsmartsky.penmaji.SerialPort.FileOperation;

public class PrintSetActivity extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private Button print_btn_confirm;
    private int[] addrarray;
    private byte[] valuearray;
    private EditText print_edt_jjcs;//间距参数====延时参数
    private EditText print_edt_settime;//设定时间
    private EditText dprint_edt_pysd;
    private EditText print_edt_jpcs;
    private FileOperation fileOperation = new FileOperation();
    private TextView print_tv_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_set);
        Init();
        FindViewById();
    }

    //初始化
    private void Init() {
        //地址从小到大
        addrarray = new int[]{0x618, 0x628, 0x629, 0x62a, 0x62b, 0x638, 0x639, 0x63a,
                0x654, 0x668, 0x676, 0x677, 0x682, 0x692, 0x6a0, 0x6b0, 0x6b1, 0x6be, 0x6ce, 0x6dc, 0x6ec};
        valuearray = new byte[]{'A', 0, 0, 0, 0, 0, 0, 0, 'M',0, 0, 0, 'N', 'N',
                0, 0, 0, 'N', 'N', 'N', 'N'};
    }

    private void FindViewById() {
        dprint_edt_pysd = (EditText)findViewById(R.id.dprint_edt_pysd);
        print_edt_jjcs = (EditText) findViewById(R.id.print_edt_jjcs);
        print_edt_settime = (EditText) findViewById(R.id.print_edt_settime);
        print_tv_info = (TextView)findViewById(R.id.print_tv_info);
        ((Button) findViewById(R.id.print_btn_advance)).setOnClickListener(this);//高级
        ((Button) findViewById(R.id.print_btn_confirm)).setOnClickListener(this);//确定喷印
        ((Button)findViewById(R.id.print_btn_infolist)).setOnClickListener(this);//信息列表
        ((Button)findViewById(R.id.print_btn_preview)).setOnClickListener(this);//预览
        //使用定长
        ((ToggleButton) findViewById(R.id.printset_tlbtn_statedlen)).setOnCheckedChangeListener(PrintSetActivity.this);
        //使用同步
        ((ToggleButton) findViewById(R.id.printset_tlbtn_syn)).setOnCheckedChangeListener(PrintSetActivity.this);
        //报警功能
        ((ToggleButton) findViewById(R.id.printset_tlbtn_warning)).setOnCheckedChangeListener(PrintSetActivity.this);
        //扬声器
        ((ToggleButton) findViewById(R.id.printset_tlbtn_loud)).setOnCheckedChangeListener(PrintSetActivity.this);
        //保持计数
        ((ToggleButton) findViewById(R.id.printset_tlbtn_count)).setOnCheckedChangeListener(PrintSetActivity.this);
        //喷印延后
        ((ToggleButton) findViewById(R.id.printset_tlbtn_forward)).setOnCheckedChangeListener(PrintSetActivity.this);
        //探测喷印
        ((ToggleButton) findViewById(R.id.printset_tlbtn_probeprint)).setOnCheckedChangeListener(PrintSetActivity.this);
        //自动控制
        ((ToggleButton) findViewById(R.id.printset_tlbtn_autocont)).setOnCheckedChangeListener(PrintSetActivity.this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.print_btn_advance: {
                intent = new Intent(PrintSetActivity.this, AdvancadSetUpActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.print_btn_confirm: {
                valuearray[1] = (byte) ((Integer.valueOf(print_edt_jjcs.getText().toString())) / 1000);//千
                valuearray[2] = (byte) ((Integer.valueOf(print_edt_jjcs.getText().toString())) / 100 % 10);//百
                valuearray[3] = (byte) ((Integer.valueOf(print_edt_jjcs.getText().toString())) / 10 % 10);//十
                valuearray[4] = (byte) ((Integer.valueOf(print_edt_jjcs.getText().toString())) % 10);//个
                valuearray[5] = (byte) ((Integer.valueOf(print_edt_settime.getText().toString())) / 100 % 10);//百
                valuearray[6] = (byte) ((Integer.valueOf(print_edt_settime.getText().toString())) / 10 % 10);//十
                valuearray[7] = (byte) ((Integer.valueOf(print_edt_settime.getText().toString())) % 10);//个
                valuearray[9] =  (byte) ((Integer.valueOf(dprint_edt_pysd.getText().toString()))/1);//?????????????
                valuearray[14] =  (byte) ((Integer.valueOf(print_edt_jpcs.getText().toString()))/1);//?????????????
                FileOperation fileOperation = new FileOperation();
                fileOperation.setDataBlockValues(addrarray, valuearray);
//                len = 0;
                break;
            }
            case R.id.print_btn_infolist:{
                CreatDialog();
                break;
            }
            case R.id.print_btn_preview:{
                if(filename != null){
                    bundle.putString("filename", filename);
                    intent.putExtras(bundle);
                    intent.setClass(PrintSetActivity.this, DrawActivity.class);
                    startActivity(intent);
                }
                break;
            }
        }
    }
private String filename = "";
    private void CreatDialog(){
        final String filenames[];
        filenames = fileOperation.getAllFilenames();
        AlertDialog.Builder builder = new AlertDialog.Builder(PrintSetActivity.this);
        builder.setTitle("请选择文件");
        builder.setItems(filenames, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                filename = filenames[which];
                print_tv_info.setText(filename + "（点击'预览'查看图形）");
            }
        });
        builder.create().show();
    }

    //开关选项
    private void IsChecked(boolean isChecked, String contentstr, int address, int index) {
        if (isChecked) {
            Log.i("fd", contentstr + "yes" );
            if (index == 0) {
                addrarray[index] = address;
                valuearray[index] = 'D';//探测喷印
                Toast.makeText(PrintSetActivity.this, "已设置为探测喷印", 2000).show();
            } else if (index == 8) {
                addrarray[index] = address;
                valuearray[index] = 'A';//自动控制
                Toast.makeText(PrintSetActivity.this, "已设置为自动控制", 2000).show();
            } else {
                addrarray[index] = address;
                valuearray[index] = 'Y';
                Toast.makeText(PrintSetActivity.this, contentstr + " 已设置为打开", 3000).show();
            }

        } else {
            Log.i("fd", contentstr + "no" );
            if (index == 0) {
                addrarray[index] = address;
                valuearray[index] = 'A';//自动喷印
                Toast.makeText(PrintSetActivity.this, "已设置为自动喷印", 2000).show();
            }else if(index == 8){
                addrarray[index] = address;
                valuearray[index] = 'M';//手动控制
                Toast.makeText(PrintSetActivity.this, "已设置为手动控制", 2000).show();
            }
            else {
                addrarray[index] = address;
                valuearray[index] = 'N';
                Toast.makeText(PrintSetActivity.this, contentstr + " 已设置为关闭", 3000).show();
            }

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            //探测喷印
            case R.id.printset_tlbtn_probeprint:{
                IsChecked(isChecked, "探测喷印", 0x6ec, 0);
                break;
            }
            //使用定长
            case R.id.printset_tlbtn_statedlen: {
                IsChecked(isChecked, "定长", 0x6be, 17);
                break;
            }
            //使用同步
            case R.id.printset_tlbtn_syn: {
                IsChecked(isChecked, "同步", 0x6ce, 18);
                break;
            }
            case R.id.printset_tlbtn_warning: {
                IsChecked(isChecked, "报警功能", 0x682, 12);
                break;
            }
            case R.id.printset_tlbtn_loud: {
                IsChecked(isChecked, "喇叭功能", 0x692, 13);
                break;
            }
            //定长喷印时，连续计数功能打开/关闭(Yes/No)
            case R.id.printset_tlbtn_count: {
//                if (print_Ibt_defin.isChecked()) {
//                    IsChecked(isChecked, "保持计数", 0x6dc, 19);
//                } else {
//                    Toast.makeText(PrintSetActivity.this, "请将定长喷印功能打开", 3000).show();
//                }
                break;
            }
            case R.id.printset_tlbtn_forward: {
                IsChecked(isChecked, "喷印延后", 0x6ec, 20);
                break;
            }

            case R.id.printset_tlbtn_autocont: {
                IsChecked(isChecked, "自动控制", 0x654, 8);
                break;
            }

        }
    }
}
